<?php

/**
 * This is the model class for table "category".
 *
 * The followings are the available columns in table 'category':
 * @property integer $cat_id
 * @property string $cat_name
 * @property string $cat_title
 * @property string $cat_des
 * @property integer $cat_parent_id
 * @property integer $cat_position
 * @property integer $cat_active
 */
class Category extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'category';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('cat_name', 'required','message' => '{attribute} không được bỏ trống'),
			array('cat_parent_id, cat_position', 'numerical', 'integerOnly'=>true),
			array('cat_name', 'length', 'max'=>100),
			array('cat_title, cat_des', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('cat_id, cat_name, cat_title, cat_des, cat_parent_id, cat_position', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cat_id' => 'ID',
			'cat_name' => 'Tên danh mục',
			'cat_title' => 'Title (Seo)',
			'cat_des' => 'Mô tả (Seo)',
			'cat_parent_id' => 'Danh mục cha',
			'cat_position' => 'Vị trí',
         'cat_active' => 'Hiển thị',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('cat_id',$this->cat_id);
		$criteria->compare('cat_name',$this->cat_name,true);
		$criteria->compare('cat_title',$this->cat_title,true);
		$criteria->compare('cat_des',$this->cat_des,true);
		$criteria->compare('cat_parent_id',$this->cat_parent_id);
		$criteria->compare('cat_position',$this->cat_position);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
   /**
    * Get all category
   */
   public function getAll(){
      $return = array();
      $object = $this->findAll();
      if(count($object) > 0){
         foreach($object as $item){
            $return[$item['cat_id']] = $item->attributes;
         }
      }
      return $return;
   }
   
   /**
    * Lay danh sach cat dang cay. Su dung de quy
   */
   public function getTree($all_cate, $parent_id = 0, $trees = array(0 => 'Không chọn'),$delimiter = ''){
      if(!$trees) 
      {
      $trees = array();
      }
      foreach($all_cate as $key=>$value){
         if($value['cat_parent_id'] == $parent_id){
            $trees[$value['cat_id']] = $delimiter.$value['cat_name'];
            $trees = $this->getTree($all_cate,$value['cat_id'],$trees,$delimiter.'---');
         }
            
      }
      return $trees;
   }
   
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Category the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
